using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;

public class 微信整理加载触发
{
    [InitializeOnLoadMethod]    
    static void OnLoad()
    {
        
        // https://developers.weixin.qq.com/minigame/dev/guide/game-engine/unity-webgl-transform.html
        string path = "Assets/WX-WASM-SDK-V2";
        if (Directory.Exists(path))
        {
            // Debug.LogError("有微信包");
        }
        else
        {
            Debug.LogError("未加载微信包");
            Debug.LogError($"前往 https://developers.weixin.qq.com/minigame/dev/guide/game-engine/unity-webgl-transform.html 下载");
        }

        bool refresh = false;
        
        /// 程序集合引用
        var all =AssetDatabase.FindAssets("t:asmdef");
        var allpath = all.Select(s => AssetDatabase.GUIDToAssetPath(s)).ToList();
        var f微信 = allpath.FindIndex(f=>f.Contains("WxWasmSDKRuntime.asmdef"));
        var f整理 = allpath.FindIndex(f=>f.Contains("微信整理.asmdef"));
        var content = File.ReadAllText(allpath[f整理]);
        Asmdef整理 data = JsonConvert.DeserializeObject<Asmdef整理>(content);
        if (data.references == null)
        {
            data.references = new List<string>();
        }

        string target = "GUID:" + all[f微信];
        if (!data.references.Contains(target))
        {
            data.references.Add(target);

            string newcontetn = JsonConvert.SerializeObject(data);
            JToken j = JToken.Parse(newcontetn);
            string n = j.ToString(Formatting.Indented);
            File.WriteAllText(allpath[f整理],n);
            refresh = true;
            
        }
        
        // assemdef
        
        /// 宏配置
        ///
        /// 
        BuildTargetGroup buildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;

        // 获取当前的宏定义
        string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);

        // // 打印当前的宏定义
        // Debug.Log("Current Defines: " + defines);

        // 添加新的宏定义
        if (!defines.Contains("微信小游戏"))
        {
            defines += ";微信小游戏";
            refresh = true;
            
            // 设置新的宏定义
            PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, defines);
            
            // 打印更新后的宏定义
            Debug.Log("Updated Defines: " + PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup));
        }

        // if (!defines.Contains("DEBUG_MODE"))
        // {
        //     defines += ";DEBUG_MODE";
        // }

        // // 移除不需要的宏定义
        // defines = defines.Replace("OLD_DEFINE;", "");


        if (refresh)
        {
            
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
        }
    }
    
    
    
    
    [Serializable]
    private class Asmdef整理
    {
        public string name;
        public string rootNamespace;
        public List<string> references;
        public List<string> includePlatforms;
        public List<string> excludePlatforms;
        public List<string> precompiledReferences;
        public List<string> defineConstraints;
        public List<string> versionDefines;
        public bool allowUnsafeCode;
        public bool overrideReferences;
        public bool autoReferenced;
        public bool noEngineReferences;
    }
}

